﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MensUnoShop.Models;
using SenerPro;

namespace MensUnoShop.Filters
{
    /// <summary>
    /// 過濾賣場相關屬性，並建立賣場及當下類別物件
    /// </summary>
    public class MagazineMarketFilter : ActionFilterAttribute
    {
        private Logger _log;

        public MagazineMarketFilter()
        {
            _log = new Logger(Config.logpath);
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            MagazineMarket mk = null;
            Guid mkguid;
            List<Tag> navlist = new List<Tag>();
            string mksn = filterContext.ActionParameters["id"] as string;
            //建立賣場物件
            mk = MarketFactory.GetMagazineMarket(mksn);
            if (mk != null)
            {
                    filterContext.Controller.ViewData["market"] = mk;

                    Tag tag = TagFactory.GetTag(mk.TAGID);
                    if (tag != null)
                    {
                        navlist = TagFactory.GetNavTagList(mk.TAGID);
                        filterContext.Controller.ViewData["navlist"] = navlist;
                        filterContext.Controller.ViewData["currenttag"] = tag;
                    }
                    else
                    {
                        _log.RecordException(_log.FlowLogPath, "tag is not correct");
                        filterContext.Result = new RedirectResult("/");
                    }
            }
            else
            {
                _log.RecordException(_log.FlowLogPath, "market is not correct");
                filterContext.Result = new RedirectResult("/");
            }

            base.OnActionExecuting(filterContext);
        }
    }
    
    

   

}